﻿Public Module ModLogging

#Region "Subs"
    'logging of a event
    Public Sub LogEvent(ByVal IsString As String, Optional ByVal IbAddDateTime As Boolean = True, Optional ByVal IbShowMessage As Boolean = True)
        Dim LsErrorText As String = IIf(IbAddDateTime, GetTodaysDateTimeString() & ", " & IsString, IsString)

        frmMainForm.LogFile.SaveStringToFile(LsErrorText)
        If IbShowMessage Then ShowErrorMessage(LsErrorText)
    End Sub
    'logging of a event made from exception
    Public Sub LogEventFromException(ByVal IoException As Exception, Optional ByVal IsText As String = "", Optional ByVal IbAddDateTime As Boolean = True, Optional ByVal IbShowMessage As Boolean = True)
        Dim LsText As String = IIf(IsText = "", IsText, " - " & IsText)
        Dim LsErrorText As String = IIf(IbAddDateTime, GetTodaysDateTimeString() & ", " & MakeTextFromException(IoException) & LsText, MakeTextFromException(IoException) & LsText)

        frmMainForm.LogFile.SaveStringToFile(LsErrorText)
        If IbShowMessage Then ShowErrorMessage(LsErrorText)
    End Sub
    'show error message
    Private Sub ShowErrorMessage(ByVal IsText As String, Optional ByVal IsCaption As String = "Chyba!")
        MessageBox.Show(IsText, IsCaption, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub
    '' ''show error message made from exception
    ' ''Public Sub ShowErrorMessageFromError(ByVal IoException As Exception, Optional ByVal IsCaption As String = "Chyba!", Optional ByVal IsText As String = "")
    ' ''    Dim LsText As String = IIf(IsText = "", MakeTextFromException(IoException), MakeTextFromException(IoException) & vbNewLine & IsText)
    ' ''    MessageBox.Show(LsText, IsCaption, MessageBoxButtons.OK, MessageBoxIcon.Error)
    ' ''End Sub
    '' ''show exclamation message
    ' ''Public Sub ShowExclamationMessage(ByVal IsText As String, Optional ByVal IsCaption As String = "Varovanie")
    ' ''    MessageBox.Show(IsText, IsCaption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    ' ''End Sub
    '' ''show information message
    ' ''Public Sub ShowInformationMessage(ByVal IsText As String, Optional ByVal IsCaption As String = "Informacia")
    ' ''    MessageBox.Show(IsText, IsCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
    ' ''End Sub
#End Region
#Region "Functions"
    '' ''show question message
    ' ''Public Function ShowQuestionMessage(ByVal IsText As String, Optional ByVal IsCaption As String = "Otazka", Optional ByVal IoMessageBoxButtons As MessageBoxButtons = MessageBoxButtons.YesNo) As System.Windows.Forms.DialogResult
    ' ''    Return MessageBox.Show(IsText, IsCaption, IoMessageBoxButtons, MessageBoxIcon.Question)
    ' ''End Function
    'makes text string from exception attributes
    Private Function MakeTextFromException(ByVal IoException As Exception)
        If IoException IsNot Nothing Then
            Return IoException.Message & " --> " & IoException.TargetSite.ToString
        Else
            Return ""
        End If
    End Function
#End Region

End Module
